package appapi.service.impl;

import appapi.Global.BusinessException;
import appapi.Request.EquipOperPageReq;
import appapi.Request.EquipmentOperListReq;
import appapi.Vo.GetEquipOperPageVo;
import appapi.dao.CertificateMapper;
import appapi.dao.EquipoperatorMapper;
import appapi.dao.StaffMapper;
import appapi.dto.DelEquipOperDto;
import appapi.entity.Zhongjiao.CertificateDto;
import appapi.entity.Zhongjiao.EquipoperatorDto;
import appapi.entity.basic.UserInfo;
import appapi.service.IEquipoperatorService;
import appapi.utils.JwtUtil;
import appapi.utils.StringUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author djq
 * @date 2023/5/17 14:25
 * @description:
 */
@Component
public class EquipoperatorService implements IEquipoperatorService {
    @Resource
    private EquipoperatorMapper equipoperatorMapper;
    @Resource
    private CertificateMapper certificateMapper;
    @Resource
    private StaffMapper staffMapper;


    @Override
    public List<Map<String, Object>> SelectList(Long equipId) {

        List<Map<String, Object>> list = equipoperatorMapper.selectList(equipId);
        list.forEach(map -> {
            //查到该设备下所有人员的准操作项目并拼接为字符串
            List<CertificateDto> operId = certificateMapper.selectAllByOperId((Long) map.get("operId"));
            String items = operId.stream().map(CertificateDto::getOperItemName).collect(Collectors.joining(","));

            map.put("operItems", items);
        });

        return list;
    }

    @Override
    public void InsertEquipmentOper(EquipmentOperListReq req) {
        if (req.getOperIds().size() > 0) {
            if (equipoperatorMapper.selectAllByOperId(req.getOperIds(),req.getEquipId()).size()>0) {
                throw new BusinessException(-1,"该人员已存在!");
            }
        }

        req.getOperIds().forEach((operId) -> {
            EquipoperatorDto dto = new EquipoperatorDto();
            dto.setEquipId(req.getEquipId());
            dto.setOperId(operId);
            dto.setEquipNo(req.getEquipNo());
            dto.setCreateTime(LocalDateTime.now());
            equipoperatorMapper.insertSelective(dto);
        });
    }

    @Override
    public void DelEquipmentOper(DelEquipOperDto dto) {
        if (dto.getEquipOperId().size()>0){
            equipoperatorMapper.deleteList(dto.getEquipOperId());
        }
    }

    /**
     * 查询设备-操作人员
     *
     * @param request
     * @param dto
     * @return
     */
    @Override
    public List<GetEquipOperPageVo> SelectPage(HttpServletRequest request, EquipOperPageReq dto) {

        PageHelper.startPage(dto.getCurrPage(), dto.getPageSize());

        List<GetEquipOperPageVo> list = equipoperatorMapper.selectPage(dto);

        list.forEach(map -> {
            //查到该设备下所有人员的准操作项目并拼接为字符串
            List<CertificateDto> operId = certificateMapper.selectAllByOperId(map.getOperId());
            String items = operId.stream().map(CertificateDto::getOperItemName).collect(Collectors.joining(","));

            map.setOperItemName(items);
        });

        return list;
    }

    @Override
    public List<GetEquipOperPageVo> SelectPage(UserInfo userInfo, EquipOperPageReq dto) {
        //如果什么都没传,则查到该账号的所属机构显示数据
        if (dto.getOrgaPathIds() == null) {
            //拿到人员信息
            Map<String, Object> staffDto = staffMapper.selectStaffDetail(userInfo.getUserid());
            String orgaPathIds = staffDto.get("orgaPathIds").toString();
            dto.setOrgaPathIds(orgaPathIds);
        }
        PageHelper.startPage(dto.getCurrPage(), dto.getPageSize());

        List<GetEquipOperPageVo> list = equipoperatorMapper.selectPage(dto);
        for (GetEquipOperPageVo vo : list) {
            int age= LocalDateTime.now().getYear() - vo.getOperBirthday().getYear();
            vo.setOperAge(age);
        }

        list.forEach(map -> {
            //查到该设备下所有人员的准操作项目并拼接为字符串
            List<CertificateDto> operId = certificateMapper.selectAllByOperId(map.getOperId());
            String items = operId.stream().map(CertificateDto::getOperItemName).collect(Collectors.joining(","));

            map.setOperItemName((StringUtil.isNotBlank(items) || items.equals("null")) ? items : "");
        });

        return list;
    }

    @Override
    public List<GetEquipOperPageVo> getList(EquipOperPageReq req) {
        List<GetEquipOperPageVo> list = equipoperatorMapper.selectPage(req);
        return list;
    }
}
